﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerPhieuChiDao : IPhieuChiDao
    {
        public SqlServerPhieuChiDao()
        {
            SQLUtility.TableName = "PhieuChi";
        }

        //Trả về đối tượng báo ăn
        private PhieuChi MakePhieuChi(DataRow row)
        {
            Guid _id = Guid.Parse(row["ID"].ToString());
            string _so = row["So"].ToString();
            DateTime _ngay = DateTime.Parse(row["Ngay"].ToString());
            string _dienGiai = row["DienGiai"].ToString();
            decimal _tongTien = decimal.Parse(row["TongTien"].ToString());
            Guid _maNV = Guid.Parse(row["MaNV"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());

         return new PhieuChi(_id, _so, _ngay, _dienGiai, _tongTien, _maNV, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<PhieuChi> MakePhieuChis(DataTable dt)  
        {
            IList<PhieuChi> list = new List<PhieuChi>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakePhieuChi(row));
            }
            return list;
        }
        private DbParameter[] Take(PhieuChi PhieuChi)
        {
            DbParameter[] parameters = new DbParameter[7];
            parameters[0] = new SqlParameter("@ID", PhieuChi.Id);
            parameters[1] = new SqlParameter("@So", PhieuChi.So);
            parameters[2] = new SqlParameter("@Ngay", PhieuChi.Ngay);

            parameters[3] = new SqlParameter("@DienGiai", PhieuChi.DienGiai);
            parameters[4] = new SqlParameter("@TongTien", PhieuChi.TongTien);
            parameters[5] = new SqlParameter("@MaNV", PhieuChi.MaNV);

            parameters[6] = new SqlParameter("@SchoolID", PhieuChi.SchoolID);

            return parameters;
        }
        public IList<Business_Objects.PhieuChi> GetPhieuChi()
        {
            SQLUtility.TableName = "PhieuChi";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakePhieuChis(dt);
        }

        public Business_Objects.PhieuChi GetPhieuChiByID(string PhieuChiID)
        {
            SQLUtility.TableName = "PhieuChi";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@ID", PhieuChiID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakePhieuChi(row);
            }
        }

        public void InsertPhieuChi(Business_Objects.PhieuChi PhieuChi)
        {
            SQLUtility.TableName = "PhieuChi";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(PhieuChi), true);
        }

        public int UpdatePhieuChi(Business_Objects.PhieuChi PhieuChi)
        {
            SQLUtility.TableName = "PhieuChi";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(PhieuChi)); }
            catch { return -1; }
        }

        public int DeletePhieuChi(Business_Objects.PhieuChi PhieuChi)
        {
            SQLUtility.TableName = "PhieuChi";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(PhieuChi)); }
            catch { return -1; }
        }
    }
}
